/**
 * 正式订单维护
 * @author shixc
 * @date 2019/01/18
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table1,
        table_gwam = {GWCSACTI_LIST: "N"},
        table_gwan = {GWAN001: ''},
        gwam001,
        cols = [[
            {field: 'ID', type: 'checkbox', title: '选择', fixed: 'left'}
            , {field: 'GWCSDOCNO', width: 160, title: '订单编号', search: 'default',searchOpts: {icon: true},  fixed: 'left', sort: true}
            , {
                field: 'GWCSDOCDATE',
                width: 80,
                title: '单据日期',
                search: 'default',
                searchOpts: {type: 'daterange'},
                sort: true
            }
            , {field: 'GWCS010', width: 80, title: '客户编号', search: 'default', searchOpts: {icon: true}, sort: true}
            , {field: 'GWAKL003', width: 100, title: '客户名称'}
            , {field: 'GWCS014', width: 150, title: '销售人员ID', sort: true}
            , {field: 'GWCS014_NAME', width: 80, title: '销售人员名称'}
            , {field: 'GWCS114', width: 80, title: '最终客户', sort: true}
            , {field: 'GWCS045', width: 80, title: 'ERP订单号', sort: true}
            , {field: 'GWCS050', width: 80, title: '备注', sort: true}
            , {
                field: 'GWCSACTI', width: 60, title: '状态码',filterDatas: app.datas_('gwai710_gwcsacti')
            }
            , {
                width: 60,
                title: '',
                toolbar: '#script-gwai710setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwai710SettingAction
    ;

    var Gwai710 = app.Module.extend({
        init: function () {
            self = this;
            var _searchsArr = app.cloneJSON(cols[0]);
            self._super({
                _tag: 'gwai710',
                _searchs: [{
                    id: 'gwai710', tableId: 'gwai710_table', datas: _searchsArr.pushArray([
                        {
                            field: 'GWCS001',
                            title: '产品编号',
                            search: 'default',
                            searchOpts: {icon: true},
                            event: 'openGwam001'
                        }
                        , {
                            field: 'GWCS109',
                            title: '活动编号',
                            search: 'default',
                            searchOpts: {icon: true},
                            event: 'openGwdc001'
                        },
                        {
                            field: 'GWCSACTI', width: 60, title: '状态码', search: 'default',
                            searchOpts: {
                                type: 'selectmultiple',
                                colX: 2,
                                default: ['N'],
                                data: app.datas_('gwai710_gwcsacti')
                            }
                        }
                    ])
                }],
                _loadTplsFull: [{
                    dom: 'gwai710_edit', tpl: '/gwai710_edit'
                },
                    {
                        dom: 'gwai710_ERP_result', tpl: '/gwai710_ERP_result'
                    }, {
                        dom: 'common_open_window', tpl: 'common/_open_window'
                    },{
                        dom: 'common_open_gwdc001', tpl: 'common/_open_gwdc001'
                    }
                ]
            }, win);
        },

        initTable: function () {
            // var sort = {"field":"GWCSDATE","sort":"desc","field":"GWCSTIME","sort":"desc"};
            // table_gwam['_SORT'] = JSON.stringify(sort);
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwai710_table'),
                    url: g_ctxPath + 'crm/gwai710/getList',
                    exportsUrl: g_ctxPath + 'crm/gwai710/exportsData',
                    importsUrl: g_ctxPath + 'crm/gwai710/importsData',
                    exportsMbUrl: g_ctxPath + 'files/订单批量导入模板.xlsx',
                    exportsName: '订单资料数据',
                    title: '订单资料信息表',
                    method: 'post',
                    id: 'gwai710_table',
                    initSort: {field: 'GWCSDOCNO', type: 'desc'},
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter'],//, 'exports_import'
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: table_gwam,
                    done: function (res, curr, count) {

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-gwai710setting-action').innerHTML).render({}, function (string) {
                    gwai710SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwai710_table)', function (obj) {
                    if (obj.event === 'img') {

                    } else {
                        layer.tips(gwai710SettingAction, obj.td, {
                            tips: [3, '']
                            , time: 3000
                            , anim: 500
                            , maxWidth: 120
                            , skin: 'layui-table-tips'
                            , success: function (layero, index) {
                                layero.find('li').on('click', function () {
                                    var isModify = true;
                                    if (obj.data.GWCSACTI == "T") {//待提交
                                        isModify = false;
                                    } else {
                                        isModify = true;
                                    }
                                    switch ($(this).attr('inx')) {
                                        case '0':
                                            if (obj.data.GWCSACTI != 'T') {
                                                app.showWarning("只有状态为T：未提交的正式订单才可以编辑！");
                                                return;
                                            }
                                            self._openTplFull({
                                                tag: 'gwai710_edit',
                                                tagData: {
                                                    is_eidt: true,
                                                    obj: obj,
                                                    is_check: false,
                                                    is_modify: isModify
                                                },
                                                index: 0,
                                                openOpts: {
                                                    title: '订单资料编辑',
                                                    maxmin: true
                                                }
                                            });
                                            break;
                                        case '1':
                                            if (obj.data.GWCSACTI != 'T') {
                                                app.showWarning("只有状态为T：未提交的正式订单才可以删除！");
                                                return;
                                            }
                                            self.del(obj);
                                            break;
                                    }
                                    layer.close(index);
                                });
                            }
                        });
                    }
                });

                //监听行双击事件
                table.on('rowDouble(gwai710_table)', function (obj) {
                    var isModify = true;
                    if (obj.data.GWCSACTI == "T") {
                        isModify = false;
                    } else {
                        isModify = true;
                    }
                    self._openTplFull({
                        tag: 'gwai710_edit',
                        tagData: {
                            is_check: true,
                            is_modify: isModify,
                            obj: obj,
                            isPuhuo: false,
                            notPuhuo: false
                        },
                        index: 0,
                        openOpts: {
                            title: '查看订单资料',
                            maxmin: true
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwai710_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwai710_edit',
                                tagData: {
                                    is_add: true,
                                    isPuhuo: true,
                                    notPuhuo: false

                                },
                                openOpts: {
                                    title: '订单新增',
                                    maxmin: true
                                },
                                index: 0
                            });
                            break;
                        case 'puhuo':
                            self._openTplFull({
                                tag: 'gwai710_edit',
                                tagData: {
                                    is_add: true,
                                    isPuhuo: false,
                                    notPuhuo: true
                                },
                                openOpts: {
                                    title: '铺货订单新增',
                                    maxmin: true
                                },
                                index: 0
                            });
                            break;
                        case 'del':
                            app.confirm({
                                msg: '是否删除选择的数据？',
                                ok: function () {
                                    self.deleteDataBatch();
                                }
                            });
                            break;
                        case 'check':
                            self.check();
                            break;
                        case 'unCheck':
                            self.unCheck();
                            break;
                        case 'transferERP':
                            self.transfer();
                            break;
                        case 'restoreERP':
                            self.restoreERP();
                            break;
                        case 'commit':
                            self.commit();
                            break;
                        case 'unCommit':
                            self.unCommit();
                            break;
                        case 'cancel':
                            self.cancel();
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwai710_table', where: table_gwam});
        },

        del: function (obj) {
            app.confirm({
                msg: '是否删除选择的信息？',
                ok: function () {
                    app.ajax({
                        xurl: 'crm/gwai710/del',
                        xdata: obj.data,
                        loading: true,
                        okmsg: '正式订单信息删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            });

        },

        deleteDataBatch: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwai710_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('GWCS');
                    if (v.GWCSACTI != 'T') {
                        app.showWarning("只有状态为T：未提交的正式订单才可以删除！");
                        flag = false;
                        return;
                    } else {
                        _temp.GWCSENT = v.GWCSENT;
                        _temp.GWCSDOCNO = v.GWCSDOCNO;
                        _xdata.push(_temp);
                    }
                });
                if (flag) {
                    app.ajax({
                        xurl: 'crm/gwai710/deleteDataBatch',
                        xdata: _xdata,
                        loading: true,
                        toJsonStr: true,
                        okmsg: '正式订单信息删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            }
        },

        check: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwai710_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('GWCS');
                    if (v.GWCSACTI != 'N') {
                        app.showWarning("只有状态为N：已提交的正式订单才可以审核！");
                        flag = false;
                        return;
                    } else {
                        _temp.GWCSENT = v.GWCSENT;
                        _temp.GWCSDOCNO = v.GWCSDOCNO;
                        _temp.GWCSACTI = "Y";
                        _xdata.push(_temp);
                    }
                });
            } else {
                app.showWarning("请先选择单据！");
                return;
            }
            if (flag) {
                app.confirm({
                    msg: '是否审核选择的数据？',
                    ok: function () {
                        app.ajax({
                            xurl: 'crm/gwai710/updateData',
                            xdata: _xdata,
                            loading: true,
                            toJsonStr: true,
                            okmsg: '正式订单审核成功',
                            cb: function (result) {
                                if (result.ok) {
                                    self.reloadTableCurr();
                                }
                            }
                        });
                    }
                });
            }
        },

        unCheck: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwai710_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('GWCS');
                    if (v.GWCSACTI != 'Y') {
                        app.showWarning("只有状态为Y：已审核的正式订单才可以取消审核！");
                        flag = false;
                        return;
                    } else {
                        _temp.GWCSENT = v.GWCSENT;
                        _temp.GWCSDOCNO = v.GWCSDOCNO;
                        _temp.GWCSACTI = "N";
                        _xdata.push(_temp);
                    }
                });
            } else {
                app.showWarning("请先选择单据！");
                return;
            }
            if (flag) {
                app.confirm({
                    msg: '是否取消审核选择的数据？',
                    ok: function () {
                        app.ajax({
                            xurl: 'crm/gwai710/updateData',
                            xdata: _xdata,
                            loading: true,
                            toJsonStr: true,
                            okmsg: '正式订单取消审核成功',
                            cb: function (result) {
                                if (result.ok) {
                                    self.reloadTableCurr();
                                }
                            }
                        });
                    }
                });
            }
        },

        transfer: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwai710_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    if (v.GWCSACTI != 'Y') {
                        app.showWarning("只有已审核的正式订单才可以转ERP订单!");
                        flag = false;
                        return;
                    } else {
                        _xdata.push(v.GWCSDOCNO);
                    }
                });
            } else {
                app.showWarning("请先选择单据！");
                return;
            }
            if (flag) {
                app.confirm({
                    msg: '是否将选择的单据转为ERP订单？',
                    ok: function () {
                        app.ajax({
                            xurl: 'crm/gwai710/transfer',
                            xdata: {datas: _xdata.toString()},
                            cb: function (result) {
                                if (result.ok) {
                                    self.reloadTableCurr();
                                    self._openTplFull({
                                        tag: 'gwai710_ERP_result',
                                        tagData: {
                                            list: result.data.data
                                        },
                                        index: 1,
                                        openOpts: {
                                            title: '转ERP订单结果',
                                            area: ['800px', '400px']
                                        }
                                    });
                                }
                            }
                        });
                    }
                });
            }
        },

        restoreERP: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwai710_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    if (v.GWCSACTI != 'Z') {
                        app.showWarning("未转ERP订单，不能操作!");
                        flag = false;
                        return;
                    } else {
                        _xdata.push(v.GWCSDOCNO);
                    }
                });
            } else {
                app.showWarning("请先选择单据！");
                return;
            }
            if (flag) {
                app.confirm({
                    msg: '是否将选择的单据撤销？',
                    ok: function () {
                        app.ajax({
                            xurl: 'crm/gwai710/restore',
                            xdata: {datas: _xdata.toString()},
                            cb: function (result) {
                                if (result.ok) {
                                    self.reloadTableCurr();
                                    app.showMessage(result.data.data.docno);
                                }
                            }
                        });
                    }
                });
            }
        },

        commit: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwai710_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('GWCS');
                    if (v.GWCSACTI != 'T') {
                        app.showWarning("只有状态为T：未提交的正式订单才可以提交！");
                        flag = false;
                        return;
                    } else {
                        _temp.GWCSENT = v.GWCSENT;
                        _temp.GWCSDOCNO = v.GWCSDOCNO;
                        _temp.GWCSACTI = "N";
                        _xdata.push(_temp);
                    }
                });
            } else {
                app.showWarning("请先选择单据！");
                return;
            }
            if (flag) {
                app.ajax({
                    xurl: 'crm/gwai710/updateData',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    confirm: {msg: "是否提交选择的数据？"},
                    okmsg: '正式订单提交成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },

        unCommit: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwai710_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('GWCS');
                    if (v.GWCSACTI != 'N') {
                        app.showWarning("只有状态为N：已提交的正式订单才可以取消提交！");
                        flag = false;
                        return;
                    } else {
                        _temp.GWCSENT = v.GWCSENT;
                        _temp.GWCSDOCNO = v.GWCSDOCNO;
                        _temp.GWCSACTI = "T";
                        _xdata.push(_temp);
                    }
                });
            } else {
                app.showWarning("请先选择单据！");
                return;
            }
            if (flag) {
                app.ajax({
                    xurl: 'crm/gwai710/updateData',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    confirm: {msg: "是否提交选择的数据？"},
                    okmsg: '正式订单提交成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },

        cancel: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwai710_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('GWCS');
                    if (v.GWCSACTI != 'T') {
                        app.showWarning("只有状态为Z：已转ERP的正式订单才可以撤销！");
                        flag = false;
                        return;
                    } else {
                        _temp.GWCSENT = v.GWCSENT;
                        _temp.GWCSDOCNO = v.GWCSDOCNO;
                        _temp.GWCSACTI = "Y";
                        _xdata.push(_temp);
                    }
                });
            } else {
                app.showWarning("请先选择单据！");
                return;
            }
            if (flag) {
                app.ajax({
                    xurl: 'crm/gwai710/cancelData',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    confirm: {msg: "是否撤销选择的数据？"},
                    okmsg: '正式订单撤销成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },

        _ready: function (permission) {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwai710',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field == 'GWCS001') {//产品开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwam001',
                                            columnList: [
                                                {label: '产品编号', prop: 'GWAM001', width: '150'},
                                                {label: '产品名称', prop: 'GWAML003', width: '150'},
                                                {label: '规格', prop: 'GWAML004', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWCS001 = data.GWAM001;
                                        },
                                    },
                                    index: 2,
                                    openOpts: {
                                        title: '选择产品',
                                        area: ['600px', '470px']
                                    }
                                });
                            } else if (field == 'GWCS010') {//客户开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'cq_gwak001_4',
                                            columnList: [
                                                {label: '客户编号', prop: 'GWAK001', width: '150'},
                                                {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                                {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWCS010 = data.GWAK001;
                                        },
                                    },
                                    index: 2,
                                    openOpts: {
                                        title: '选择客户',
                                        area: ['600px', '470px']
                                    }
                                });
                            } else if (field == 'GWCSMLEGAL') {//组织开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'cq_gwak001_4',
                                            columnList: [
                                                {label: '组织编号', prop: 'ID', width: '150'},
                                                {label: '组织名称', prop: 'NAME', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWCSMLEGAL = data.ID;
                                        },
                                    },
                                    index: 2,
                                    openOpts: {
                                        title: '选择归属组织',
                                        area: ['600px', '470px']
                                    }
                                });
                            } else if (field === 'GWCS109') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwdc001',
                                            columnList: [
                                                {label: '活动编号', prop: 'GWDC001', width: '150'},
                                                {label: '活动说明', prop: 'GWDC003', width: '150'},
                                                {label: '促销方式', prop: 'GWDC005', width: '150'},
                                                {label: '促销单据类型', prop: 'GWDC004', width: '150'},
                                                {label: '生效日期', prop: 'GWDC009', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWCS109 = data.GWDC001;
                                        },
                                    },
                                    index: 2,
                                    openOpts: {
                                        title: '选择活动信息',
                                        area: ['600px', '470px']
                                    }
                                });
                            }else if (field === 'GWCSDOCNO') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            columnList: [
                                                {label: '订单编号', prop: 'GWCSDOCNO', width: '150'},
                                                {label: '订单日期', prop: 'GWCSDOCDATE', width: '150'},
                                                {label: '人员', prop: 'GWCS014_NAME', width: '150'},
                                                {label: '状态码', prop: 'GWCSACTI_NAME', width: '150'}
                                            ],
                                            url: 'crm/gwai710/getGwcsdocnoData'
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWCSDOCNO = data.GWCSDOCNO;
                                        },
                                    },
                                    index: 2,
                                    openOpts: {
                                        title: '选择订单单号',
                                        area: ['600px', '470px']
                                    }
                                });
                            }
                        }
                    },
                    watch: {
                        'isearch.fields.GWCSDOCDATE': function (newValue, oldValue) {
                            if (newValue != null && newValue.length == 2) {
                                this.isearch.fields.GWCSDOCDATE_START = newValue[0];
                                this.isearch.fields.GWCSDOCDATE_END = newValue[1];
                            } else {
                                this.isearch.fields.GWCSDOCDATE_START = '';
                                this.isearch.fields.GWCSDOCDATE_END = '';
                            }
                        },
                        'isearch.fields.GWCSACTI': function (newValue, oldValue) {
                            var data = '';
                            var flag = false;
                            for (var i = 0; i < newValue.length; i++) {
                                if(newValue[i] == ''){
                                    flag = true;
                                }
                                if (i == 0) {
                                    data = data + newValue[i];
                                } else {
                                    data = data + ',' + newValue[i];
                                }
                            }
                            if(flag){
                                data = '';
                            }
                            this.isearch.fields.GWCSACTI_LIST = data;
                        }
                    }
                }]
            });
            self.initTable();
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

    return new Gwai710();

});
